#include "split_log.hpp"
#include "socket.hpp"
#include "send_command.hpp"

Split_Log::Split_Log(string log,string l):limit(l),name_file(log)
{}

void Split_Log::set_limit(string l)
{
  limit=l;
}

void Split_Log::set_name_file(string f)
{
  name_file=f;
}

string Split_Log::get_limit()
{
  return limit;
}

string Split_Log::get_name_file()
{
  return name_file;
}

unsigned int Split_Log::count_limit()
{
  unsigned int r_count(0);
  FILE* p_file=fopen(name_file.c_str(),"r");
  string lign("");
  int cara(0);

  if(p_file != NULL)
    {
      fseek(p_file,0,SEEK_SET);

      do
	{
	  cara=fgetc(p_file);

	  if((char)cara != '\n')
	    {
	      lign+=(char)cara;
	    }
	  else
	    {
	      if(have_limit(lign))
		++r_count;
	      lign="";
	    }
	}while(cara != EOF);

      fclose(p_file);
    }

  return r_count;
}

void Split_Log::separet_log()
{
  FILE* p_file=fopen(name_file.c_str(),"r");
  FILE* p_out_file(NULL);
  string lign("");
  G_Name out_file(name_file);
  int cara(0);

  if(p_file != NULL)
    {
      fseek(p_file,0,SEEK_SET);

      do
	{
	  cara=fgetc(p_file);

	  if(cara != '\n')
	    lign+=cara;
	  else
	    {
	      if(have_limit(lign))
		{
		  if(p_out_file != NULL)
		    {
		      fclose(p_out_file);
		      p_out_file=NULL;
		      out_file.next_name();
		    }
		}
	      else
		{
		  if(p_out_file == NULL)
		    {
		      p_out_file=fopen(out_file.get_name().c_str(),"a+");
		    }

		  lign+="\n";
		  fwrite(lign.c_str(),sizeof(char),lign.length(),p_out_file);
		}

	      lign="";
	    }

	}while(cara != EOF);
    }
}

void Split_Log::split_by_time()
{
  FILE* p_file=fopen(name_file.c_str(),"r");
  FILE* p_out_file(NULL);
  string lign("");
  G_Name out_file(name_file);
  int cara(0);
  string tmp_limit;

  if(p_file != NULL)
    {
      fseek(p_file,0,SEEK_SET);
      tmp_limit=limit;
      limit="ShutdownGame:";

      do
	{
	  cara=fgetc(p_file);

	  if(cara != '\n')
	    lign+=cara;
	  else
	    {
	      if(have_limit(lign))
		{
		  if(p_out_file != NULL)
		    {
		      lign+="\n";
		      fwrite(lign.c_str(),sizeof(char),lign.length(),p_out_file);
		      fclose(p_out_file);
		      p_out_file=NULL;
		      out_file.next_name();
		    }
		}
	      else
		{
		  if(p_out_file == NULL)
		    {
		      p_out_file=fopen(out_file.get_name().c_str(),"a+");
		    }

		  lign+="\n";
		  fwrite(lign.c_str(),sizeof(char),lign.length(),p_out_file);
		}

	      lign="";
	    }

	}while(cara != EOF);
    }
  limit=tmp_limit;
}


//int main(/*int argc,const char* argv[]*/void)
//{
  /*
  Socket t("127.0.0.1",34444,44444);
  const char* password="test";
  string cmp="";

  for(int c=0 ; c < 4 ; ++c)
    cmp+=(char)0xFF;

  cmp=cmp+"rcon "+password+" bigtext";

  for(int c=1 ; c < argc ; ++c)
    {
      cmp+=" ";
      if(c == 1)
	cmp+="\"";
      cmp+=argv[c];
    }

  cmp+="\"";

  t.send_data(cmp);
  t.close_socket();
  */

  /*
  Send_Command s("127.0.0.1",34444,44444,"test");

  //  s.send_bigtext("pooulpe");
  sleep(1);
  s.send_slap("0");
  s.stop_send();
  */

/*
  List* l=new List();

  l->pop_element();

  cout << "NEW" << endl;
  l->add_element("a");
  cout << "add a" << endl;

  l->add_element("b");
  cout << "add b" << endl;

  delete l;
*/

  /*
  sleep(1);
  s.send_message("pooulpe");
  s.send_message("pooulpe");
  s.send_message("pooulpe");
  s.send_message("pooulpe");
  s.send_message("pooulpe");
  
  return 0;
  }*/
